package ex;

import java.util.*;

/**
 * @author: knowno
 * @create: 2025-09-16 10:23
 * @Version 1.0
 **/
public class TestHashMap2 {
    public static void main(String[] args) {
        //<String,Order>
        HashMap<String,Order> map = new HashMap<>();

        //

        map.put("100",new Order("100","a"));
        map.put("200",new Order("200","b"));
        map.put("300",new Order("300","c"));
        //key是否允许重复 => 不允许重复，后一个值会覆盖前一个值
        map.put("100",new Order("100","ccc"));
        //是否允许为null
        map.put(null,null);

        //输出
        System.out.println(map);
        //三类获取集合元素  key集合  keySet ,value集合  key-value集合
        Set keySet = map.keySet();
        System.out.println(keySet);
        Collection values = map.values();
        System.out.println(values);
        Set<Map.Entry<String,Order>> entrySet = map.entrySet();
        System.out.println(entrySet);

        //通过key获取value
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()){
            Object key = iterator.next();
            System.out.println("key:"+key+" value:"+map.get(key));
        }

        //entrySet
        Iterator<Map.Entry<String,Order>> iterator1 = entrySet.iterator();
        while (iterator1.hasNext()){
            Map.Entry<String,Order> entry = iterator1.next();
            System.out.println(entry.getKey()+" "+entry.getValue());
        }

        

    }
}

class Order{
    private String orderId;
    private String orderName;
    public Order() {
    }
    public Order(String orderId,String orderName){
        this.orderId = orderId;
        this.orderName = orderName;
    }
    public String getOrderId() {
        return orderId;
    }
    public String getOrderName() {
        return orderName;
    }
    @Override
    public String toString() {
        return "Order{" +
                "orderId='" + orderId + '\'' +
                ", orderName='" + orderName + '\'' +
                '}';
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Order order = (Order) o;
        return orderId.equals(order.orderId);
    }
    @Override
    public int hashCode() {
        return orderId.hashCode();
    }
}
